package com.yanyu.space.codegen.controller;


import cn.hutool.core.bean.BeanUtil;
import com.yanyu.space.codegen.bean.po.Template;
import com.yanyu.space.codegen.bean.po.TemplateDesc;
import com.yanyu.space.codegen.bean.vo.template.TemplateInfoVo;
import com.yanyu.space.codegen.bean.vo.template.TemplateSaveVo;
import com.yanyu.space.codegen.bean.vo.template.TemplateSearchVo;
import com.yanyu.space.codegen.service.ITemplateDescService;
import com.yanyu.space.codegen.service.ITemplateService;
import com.yanyu.space.common.core.bean.db.PageEntity;
import com.yanyu.space.common.core.bean.web.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 模板信息 前端控制器
 *
 * @author yanyu
 * @date 2020/12/1
 */
@RestController
@Validated
@Api(tags = "模板信息")
@RequestMapping("/codegen/template")
@AllArgsConstructor
public class TemplateController {

    private ITemplateService service;
    private ITemplateDescService descService;

    @GetMapping("/list")
    @ApiOperation(value = "查询模板信息")
    public R list(@Validated TemplateSearchVo vo) {
        PageEntity<Template> page = service.listPage(vo);
        return R.ok(page);
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据模板id查询模板内容")
    public R get(@PathVariable("id") String id) {
        TemplateDesc templateDesc = descService.getById(id);
        Template template = service.getById(id);
        TemplateInfoVo templateInfoVo = BeanUtil.copyProperties(template, TemplateInfoVo.class);
        templateInfoVo.setContent(templateDesc.getContent());
        return R.ok(templateInfoVo);
    }

    @PostMapping
    @ApiOperation(value = "新增模板")
    public R add(@RequestBody @Validated TemplateSaveVo vo) {
        service.add(vo);
        return R.ok();
    }

    @PutMapping
    @ApiOperation(value = "修改模板")
    public R update(@RequestBody @Validated TemplateSaveVo vo) {
        service.update(vo);
        return R.ok();
    }

    @DeleteMapping("/{ids}")
    @ApiOperation(value = "删除模板")
    public R delete(@PathVariable("ids") List<Long> ids) {
        service.removeByIds(ids);
        return R.ok();
    }
}

